﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// EdgeDetectDifference is used to work out edges by differences
    /// </summary>
    public class EdgeDetectDifference : BaseImageFilter
    {
        /// <summary>
        /// 
        /// </summary>
        protected byte m_nThreshold = 10;

        /// <summary>
        /// default constructor
        /// </summary>
        public EdgeDetectDifference()
            : this(10)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EdgeDetectDifference"/> class.
        /// </summary>
        /// <param name="_nThreshold">The _n threshold.</param>
        public EdgeDetectDifference(byte _nThreshold)
        {
            m_nThreshold = _nThreshold;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_edgeDetectDifference"></param>
        internal EdgeDetectDifference(EdgeDetectDifference _edgeDetectDifference)
        {
            this.m_nThreshold = _edgeDetectDifference.m_nThreshold;
        }

        /// <summary>
        /// gets or sets the threshold
        /// </summary>
        public byte Threshold
        {
            get { return m_nThreshold; }
            set { m_nThreshold = value; }
        }

        /// <summary>
        /// This one works by working out the greatest difference between a pixel and it's eight neighbours.
        /// The threshold allows softer edges to be forced down to black, use 0 to negate it's effect.
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap b2 = (UnsafeBitmap)_bitmap.Clone();

            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = _bitmap.BitmapData;  //b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData bmData2 = b2.BitmapData; //b2.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int nStride = bmData.Stride;
            IntPtr Scan0 = bmData.Scan0;
            IntPtr Scan02 = bmData2.Scan0;

            int nByteCount = _bitmap.BytesPerPixelCount;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                byte* p2 = (byte*)(void*)Scan02;

                int nOffset = nStride - _bitmap.Width * nByteCount; //nStride - _bitmap.Width * 3;
                int nWidth = _bitmap.Width * nByteCount; //_bitmap.Width * 3;
                int nHeight = _bitmap.Height;

                int nPixel = 0, nPixelMax = 0;

                p += nStride;
                p2 += nStride;

                int y = 0;
                int x = 0;

                for (y = 0; y < nHeight - nByteCount; ++y)
                {
                    p += nByteCount; //p += 3;
                    p2 += nByteCount; //p2 += 3;

                    for (x = nByteCount; x < nWidth - nByteCount; ++x) //for (int x = 3; x < nWidth - 3; ++x)
                    {
                        nPixelMax = Math.Abs((p2 - nStride + nByteCount)[0] - (p2 + nStride - nByteCount)[0]);    //nPixelMax = Math.Abs((p2 - nStride + 3)[0] - (p2 + nStride - 3)[0]);
                        nPixel =    Math.Abs((p2 + nStride + nByteCount)[0] - (p2 - nStride - nByteCount)[0]);       //nPixel = Math.Abs((p2 + nStride + 3)[0] - (p2 - nStride - 3)[0]);
                        if (nPixel > nPixelMax) nPixelMax = nPixel;

                        nPixel = Math.Abs((p2 - nStride)[0] - (p2 + nStride)[0]);
                        if (nPixel > nPixelMax) nPixelMax = nPixel;

                        nPixel = Math.Abs((p2 + nByteCount)[0] - (p2 - nByteCount)[0]);                           //nPixel = Math.Abs((p2 + 3)[0] - (p2 - 3)[0]);
                        if (nPixel > nPixelMax) nPixelMax = nPixel;

                        if (nPixelMax < m_nThreshold) nPixelMax = 0;

                        p[0] = (byte)nPixelMax;

                        ++p;
                        ++p2;
                    }
                    p += nByteCount + nOffset;  //3 + nOffset;
                    p2 += nByteCount + nOffset; //3 + nOffset
                }
            }

            b2.Dispose(); // b2.UnlockBits(bmData2);
            return _bitmap;
        }

        /// <summary>
        /// used this to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new EdgeDetectDifference(this);
        }
    }
}
